package com.example.demo.mapper;

import com.example.demo.entity.Product;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Delete;

import java.util.List;

@Mapper
public interface ProductMapper {

    // 查询所有产品
    @Select("SELECT * FROM products")
    List<Product> getAllProducts();

    // 根据ID查询产品
    @Select("SELECT id, category_id AS categoryId, name, description, price, images, type FROM products WHERE id = #{id}")
    Product findById(Long id);

    // 查询所有产品
    @Select("SELECT id, category_id AS categoryId, name, description, price, images, type FROM products")
    List<Product> findAll();

    // 根据分类名称查询产品
    @Select("SELECT * FROM products WHERE category_name = #{categoryName}")
    List<Product> findByCategoryName(String categoryName);

    // 插入新产品
    @Insert("INSERT INTO products(category_id, name, description, price, images, type) VALUES(#{categoryId}, #{name}, #{description}, #{price}, #{images}, #{type})")
    void save(Product product);

    // 更新产品
    @Update("UPDATE products SET category_id=#{categoryId}, name=#{name}, description=#{description}, price=#{price}, images=#{images}, type=#{type} WHERE id=#{id}")
    void update(Product product);

    // 删除产品
    @Delete("DELETE FROM products WHERE id=#{id}")
    void delete(Long id);
}
